package com.senlingspace.system.service.impl;

import com.senlingspace.common.core.domain.AjaxResult;
import com.senlingspace.common.utils.DateUtils;
import com.senlingspace.common.utils.NumberGenerator;
import com.senlingspace.common.utils.PageUtils;
import com.senlingspace.system.domain.SlkjFareSchemeDetail;
import com.senlingspace.system.domain.SlkjTicketCategory;
import com.senlingspace.system.domain.SlkjTicketLevel;
import com.senlingspace.system.mapper.SlkjFareSchemeDetailMapper;
import com.senlingspace.system.mapper.SlkjTicketCategoryMapper;
import com.senlingspace.system.mapper.SlkjTicketLevelMapper;
import com.senlingspace.system.service.ISlkjTicketLevelService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 票档管理Service业务层处理
 *
 * @author SenlingSpace
 * @date 2024-07-03
 */
@Service
public class SlkjTicketLevelServiceImpl implements ISlkjTicketLevelService {
    @Autowired
    private SlkjTicketLevelMapper slkjTicketLevelMapper;
    @Autowired
    private SlkjTicketCategoryMapper slkjTicketCategoryMapper;
    @Autowired
    private SlkjFareSchemeDetailMapper slkjFareSchemeDetailMapper;


    /**
     * 查询票档管理
     *
     * @param id 票档管理主键
     * @return 票档管理
     */
    @Override
    public SlkjTicketLevel selectSlkjTicketLevelById(Long id) {
        return slkjTicketLevelMapper.selectSlkjTicketLevelById(id);
    }

    /**
     * 查询票档管理列表
     *
     * @param slkjTicketLevel 票档管理
     * @return 票档管理
     */
    @Override
    public List<SlkjTicketLevel> selectSlkjTicketLevelList(SlkjTicketLevel slkjTicketLevel) {
        PageUtils.startPage();
        return slkjTicketLevelMapper.selectSlkjTicketLevelList(slkjTicketLevel);
    }

    /**
     * 新增票档管理
     *
     * @param slkjTicketLevel 票档管理
     * @return 结果
     */
    @Override
    public int insertSlkjTicketLevel(SlkjTicketLevel slkjTicketLevel) {
        slkjTicketLevel.setNumber(NumberGenerator.getCharacters());
        slkjTicketLevel.setStatus(0L);
        slkjTicketLevel.setCreateTime(DateUtils.getNowDate());
        return slkjTicketLevelMapper.insertSlkjTicketLevel(slkjTicketLevel);
    }

    /**
     * 修改票档管理
     *
     * @param slkjTicketLevel 票档管理
     * @return 结果
     */
    @Override
    public int updateSlkjTicketLevel(SlkjTicketLevel slkjTicketLevel) {
        slkjTicketLevel.setNumber(null);
        slkjTicketLevel.setUpdateTime(DateUtils.getNowDate());
        return slkjTicketLevelMapper.updateSlkjTicketLevel(slkjTicketLevel);
    }

    /**
     * 批量删除票档管理
     *
     * @param ids    需要删除的票档管理主键
     * @param userId 操作人ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteSlkjTicketLevelByIds(Long[] ids, Long userId) {
        List<SlkjTicketCategory> ticketCategories = slkjTicketCategoryMapper.selectSlkjTicketCategoryByLevelIds(ids);
        if (CollectionUtils.isEmpty(ticketCategories)) {
            slkjTicketLevelMapper.updateSlkjTicketLevelByIds(ids, userId);
            return AjaxResult.success("删除成功！");
        }

        Long ticketLevelId = ticketCategories.get(0).getTicketLevelId();
        long counted = slkjFareSchemeDetailMapper.countByTicketLevelId(ticketLevelId);
        if (counted == 0) {
            slkjTicketLevelMapper.updateSlkjTicketLevelByIds(new Long[]{ticketLevelId}, userId);
            slkjTicketCategoryMapper.updateSlkjTicketCategoryByTicketLevelId(ticketLevelId, userId);
            return AjaxResult.success("删除成功！");
        }

        SlkjFareSchemeDetail query = new SlkjFareSchemeDetail();
        query.setTicketLevelId(ticketLevelId);
        query.setStatus(0);
        List<SlkjFareSchemeDetail> fareSchemeDetails = slkjFareSchemeDetailMapper.selectSlkjFareSchemeDetailList(query);
        boolean could = canDeleteFareScheme(fareSchemeDetails);
        if (could) {
            slkjTicketLevelMapper.updateSlkjTicketLevelByIds(new Long[]{ticketLevelId}, userId);
            slkjTicketCategoryMapper.updateSlkjTicketCategoryByTicketLevelId(ticketLevelId, userId);
            slkjFareSchemeDetailMapper.updateSlkjFareSchemeDetailStatusByTicketLevelId(ticketLevelId);
            return AjaxResult.success("删除成功！");
        }
        return AjaxResult.error("票档已被关联使用，禁止删除！");
    }

    public boolean canDeleteFareScheme(List<SlkjFareSchemeDetail> fareSchemeDetails) {
        for (SlkjFareSchemeDetail detail : fareSchemeDetails) {
            // 检查所有价格字段是否都为null
            if (detail.getShopPrice() != null ||
                    detail.getMiniProgramPrice() != null ||
                    detail.getDouyinPrice() != null ||
                    detail.getOriginalPrice() != null) {
                // 如果任意一个字段不为null，则不能删除
                return false;
            }
        }
        // 如果所有记录的所有价格字段都为null，则可以删除
        return true;
    }

    /**
     * 删除票档管理信息
     *
     * @param id 票档管理主键
     * @return 结果
     */
    @Override
    public int deleteSlkjTicketLevelById(Long id) {
        return slkjTicketLevelMapper.deleteSlkjTicketLevelById(id);
    }

    @Override
    public int countTicketLevelByName(String name, Long id) {
        return slkjTicketLevelMapper.countTicketLevelByName(name, id);
    }

    @Override
    public List<SlkjTicketLevel> selectSlkjTicketLevelByIds(List<Long> levelIds) {
        return slkjTicketLevelMapper.selectSlkjTicketLevelListByIds(levelIds);
    }

}
